package com.gmzg.gmzgnews.news.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gmzg.gmzgnews.R;
import com.gmzg.gmzgnews.news.entries.NewsEntry;
import com.jash.networklibrary.ImageUtil;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public class HeadlineAdapter extends BaseAdapter{
    private Context context;
    private List<NewsEntry> list;
    private LayoutInflater inflater;

    public HeadlineAdapter(Context context, List<NewsEntry> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            if (getItemViewType(position) == 0) {
                convertView = inflater.inflate(R.layout.news_item_single, parent, false);
            } else {
                convertView = inflater.inflate(R.layout.news_item_multi, parent, false);
            }
            convertView.setTag(new ViewHolder(convertView));
        }
        NewsEntry entry = list.get(position);
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.title.setText(entry.getTitle());
        holder.source.setText(entry.getSource());
        ImageUtil.loadImage(holder.image, entry.getImage());
        if (getItemViewType(position) == 1) {
            List<Map<String, String>> imgextra = entry.getImgextra();
            ImageUtil.loadImage(holder.image1, imgextra.get(0).get("imgsrc"));
            ImageUtil.loadImage(holder.image2, imgextra.get(1).get("imgsrc"));
        }

        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        NewsEntry entry = list.get(position);
        if (entry.getImgextra() == null) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public void addAll(Collection<? extends NewsEntry> collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }
    public static class ViewHolder  {
        private TextView title;
        private TextView source;
        private ImageView image;
        private ImageView image1;
        private ImageView image2;

        public ViewHolder(View itemView) {
            title = ((TextView) itemView.findViewById(R.id.item_title));
            source = ((TextView) itemView.findViewById(R.id.item_type));
            image = ((ImageView) itemView.findViewById(R.id.item_image));
            image1 = ((ImageView) itemView.findViewById(R.id.item_image_1));
            image2 = ((ImageView) itemView.findViewById(R.id.item_image_2));
        }
    }

}
